#!/bin/sh

set -e

test -z "$1" && exit 0
test -f "$1" || exit 1

includes () {
	DIR=$(dirname "$1")

	for f in $(sed -ne "s|^include::\(.*\)\[\]$|$DIR/\1|p" "$1"); do
		# components_gen.adoc will contain only generated content
		case "$f" in
			*/components_gen.adoc)
			touch "$f"
			;;
		esac
		echo "$f"
		includes "$f"
	done
}

images() {
	DIR=$(dirname "$1")
	sed -ne "s|^.*image:\{1,2\}\([^[]*\)\[.*\].*$|$DIR/\1|p" "$1" | tr '\n' ' '
}

INCLUDES=$(includes "$1" | tr '\n' ' ')
IMAGES=$(images "$1")
for f in $INCLUDES; do
	IMAGES="$IMAGES $(images "$f")"
done

echo "${1%%.txt}.dep: $INCLUDES"
echo "${1%%.txt}.html: $INCLUDES $IMAGES"
echo "${1%%.txt}.pdf: $INCLUDES $IMAGES"
